<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Administrator_calendar extends MY_Administrator {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/calendar_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();		
		//Load Model
		$this->load->model('administrator_calendar_model');
    }
	
	/*Calendar Management*/
	public function content_management($module=NULL,$arg=NULL)
	{
		//Load ColorBox
		$this->data['js'][] = "jquery.colorbox-min.js";	
		$this->data['css'][] = "colorbox.css";
					
		switch($module):
			case 'add':		
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_calendar_model->content_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;

				//Load Ckeditor
				$this->load->helper('ckeditor');				
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";
				
				$this->data['content'] = "administrator/calendar/content_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);
				//Check Id exist
				$is_exist = $this->db->from('calendar')
				->where(array('calendar_id' => $id))
				->count_all_results();
				if(empty($is_exist)) show_error('This id is not information.');
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_calendar_model->content_management_edit($id);
					redirect($this->session->userdata('returnUrl'));			
				endif;
				
				//Load Ckeditor
				$this->load->helper('ckeditor');				
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";
				//Load ZeroClipboard
				$this->data['js'][] = "ZeroClipboard/ZeroClipboard.js";

				$this->data['dataCalendarImage'] = $this->db->order_by('t.calendar_image_order ASC,t.calendar_image_id ASC')->get_where('calendar_image as t',array('t.calendar_id' => $id))->result();
				$this->data['dataResult'] = $this->db->join('calendar_info as j','j.calendar_id=t.calendar_id','left')->get_where('calendar as t',array('t.calendar_id' => $id),1)->row();
	
				$this->data['content'] = "administrator/calendar/content_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;
			case 'ajax_update_image':
				$this->administrator_calendar_model->ajax_update_image();
			break;
			case 'ajax_delete_image':
				$id = intval($arg);
				$this->administrator_calendar_model->ajax_delete_image($id);
			break;			
			case 'delete':
				$this->administrator_calendar_model->content_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;												
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
					
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "calendar_id DESC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case 'create_date':
						case 'update_date':
							$orderCondition = "t.".$orderby." ".$sort;
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
					
				/*Search Condition*/
				$searchCondition = array('t.calendar_id IS NOT NULL');
				$calendar_id = $this->input->get('calendar_id');
				$calendar_subject = addslashes($this->input->get('calendar_subject'));
				$calendar_brief = addslashes($this->input->get('calendar_brief'));
				$update_by = addslashes($this->input->get('update_by'));
				if(!empty($calendar_id)) $searchCondition[] = "t.calendar_id LIKE '%".$calendar_id."%'";
				if(!empty($calendar_subject)) $searchCondition[] = "j.calendar_subject LIKE '%".$calendar_subject."%'";
				if(!empty($calendar_brief)) $searchCondition[] = "j.calendar_brief LIKE '%".$calendar_brief."%'";
				if(!empty($update_by)) $searchCondition[] = "t.update_by LIKE '%".$update_by."%'";
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?'.query_string(array('per_page'));
				$config['total_rows'] = $this->db->from('calendar as t')
				->join('calendar_info as j','j.calendar_id=t.calendar_id','INNER')
				->where(''.implode(' AND ',$searchCondition).'')
				->count_all_results();
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->select('t.*,j.calendar_subject,j.calendar_brief')->join('calendar_info as j','j.calendar_id=t.calendar_id','LEFT')->order_by($orderCondition)->get_where('calendar as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();

				$this->data['content'] = "administrator/calendar/content_management";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}
}